<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>WebSocket</title>
</head>
<body>
<script type="text/javascript">
  let websocket = new WebSocket("ws://localhost:8087/api/channel/echo");

  // 连接断开
  websocket.onclose = e => {
    console.log(`连接关闭: code=${e.code}, reason=${e.reason}`)
  }
  // 收到消息
  websocket.onmessage = e => {
    console.log(`收到消息：${e.data}`);
  }
  // 异常
  websocket.onerror = e => {
    console.log("连接异常")
    console.error(e)
  }
  // 连接打开
  websocket.onopen = e => {
    console.log("连接打开");

    // 创建连接后，往服务器连续写入3条消息
    websocket.send("sprigdoc.cn");
    websocket.send("sprigdoc.cn");
    websocket.send("sprigdoc.cn");

    // 最后发送 bye，由服务器断开连接
    websocket.send("bye");

    // 也可以由客户端主动断开
    // websocket.close();
  }
</script>
</body>
</html>